package gxprod.androidai;

import gxprod.androidai.Bubble.BubbleType;
import gxprod.androidai.regex.MatchResult;

import java.util.Random;
import java.util.Vector;

import android.R.color;
import android.speech.tts.TextToSpeech;
import android.text.Html;
import android.text.util.Linkify;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * Manages Bubbles. This will need changes to allow widgets to be displayed.
 * @author Josh Maleszewski
 */
public class BubbleAdapter extends BaseAdapter implements OnItemClickListener {

	private Vector<Bubble> items;
	private LayoutInflater mInflater; 
	public boolean isMute = false;
	private Random rand = new Random();

	public BubbleAdapter(AIActivity context) {
		items = new Vector<Bubble>();
		mInflater = LayoutInflater.from(context);
	}

	public int getCount() { return items.size();}
	public Bubble getItem(int position) { return items.get(position); }
	public long getItemId(int position) { return position; }
	public Vector<Bubble> getArray() { return items; }

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		Bubble item = items.get(position);

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.text_bubble, null);
			holder = new ViewHolder();
			holder.view = (TextView) convertView.findViewById(R.id.textView1);
			convertView.setTag(holder);
		} else 
			holder = (ViewHolder) convertView.getTag();

		TextView tv = holder.view;
		tv.setText(Html.fromHtml(item.text));
		tv.setTextColor(item.type.PrimColor);
		tv.setPadding(10, 10, 10, 10);
		tv.setGravity(Gravity.LEFT);
		tv.setBackgroundColor(color.background_dark);
		tv.setBackgroundColor(item.type.SecColor);
		tv.setGravity(item.getGravity());

		Linkify.addLinks(tv, Linkify.EMAIL_ADDRESSES | Linkify.WEB_URLS);

		return convertView;
	}

	public void addItem(Bubble active) {
		if(isMute) 
			return;
		if(items.lastElement().isTemp) 
			items.remove(items.size() - 1);
		
		items.add(active);
	}

	public void output(String text) { output(text, text); }
	public void output(String text, String speach) {
		if(isMute) 
			return;
		items.add(new Bubble(text));
		say(speach);
	}
	public void output(String text, String speach, BubbleType type) {
		if(isMute) 
			return;
		items.add(new Bubble(text, type));
		say(speach);

	}
	public void output(int stringId, boolean say, Object[] ...fillins) {
		Object o = AIActivity.get().getString(stringId);
		String out = (String)o;
		if(fillins != null)
			for(int i = 0; i < fillins.length; i++) 
				out = out.replace("&" + i, fillins[i].toString());
		addItem(new Bubble(out));
		if(say)
			say(out);
	}	
	public void output(int stringId, Object[] ...fillins) {
		output(stringId, true, fillins);
	}	

	public void outputRandom(int stringsId) {
		String[] strs = AIActivity.get().getResources().getStringArray(stringsId);
		int i = rand.nextInt(strs.length);
		output(strs[i], strs[i]);
	}
	public void outputRandom(int textId, int sayId) {
		String[] text = AIActivity.get().getResources().getStringArray(textId);
		String[] say = AIActivity.get().getResources().getStringArray(sayId);
		int i = rand.nextInt(text.length);
		output(text[i], say[i]);
	}

	public void say(String say) {
		if(isMute) return;
		if(AIActivity.get().TTSReady & say != null)
			AIActivity.get().mTts.speak(say, TextToSpeech.QUEUE_ADD, null);
	}

	public void postUpdate() {
		AIActivity.get().runOnUiThread(new Runnable() {
			public void run() { notifyDataSetChanged(); }
		});
	}
	public void postoutput(final String write, final String say) {
		if(isMute) return;
		AIActivity.get().runOnUiThread(new Runnable() {
			public void run() { output(write, say); }
		});
	}

	public void outputTemp(String string) {
		output(string);
		items.lastElement().isTemp = true;
	}	
	
	private void unmute() { isMute = false;	}
	private void mute() { isMute = true; }
	
	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
		AIActivity.get().list.setSelectionFromTop(getCount() - 1, AIActivity.get().list.getHeight() / 5 * 4);
	}
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Bubble item = items.get(arg2);
		if(item.widgetData != null) {
			if(item.widgetData instanceof MatchResult) {
				MatchResult res = ((MatchResult)item.widgetData);
				this.mute();
				res.Function.execute(res, this);
				this.unmute();
			}
		}
	}
	
	public static class ViewHolder {
		public TextView view;
	}
}

